import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/components/Login.vue' // 登陆
// import Home from '@/components/Home.vue' // 主页
// import Welcome from '@/components/Welcome' // 主页的二级页
// import Goods from '../components/goods/List.vue' // 商品列表
import Cate from '@/components/goods/Cate' // 分类参数
import Params from '@/components/goods/Params' // 商品分类
// import Oder from '@/components/order/Oder' // 订单
import Rights from '@/components/power/Rights' // 权限列表
import Roles from '@/components/power/Roles' // 角色列表
import Report from '@/components/report/Report' // 数据报表
// import Users from '@/components/users/Users' // 用户列表
// import Add from '@/components/goods/Add' // 商品添加页面

//= ===================路由懒加载======================
const Login = () => import(/* webpackChunkName: "Login" */ '@/components/Login.vue')
const Home = () => import(/* webpackChunkName: "Home" */ '@/components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Welcome" */ '@/components/Welcome')
const Goods = () => import(/* webpackChunkName: "Goods" */ '@/components/goods/List.vue')
const Oder = () => import(/* webpackChunkName: "Oder" */ '@/components/order/Oder')
const Users = () => import(/* webpackChunkName: "Users" */ '@/components/users/Users')
const Add = () => import(/* webpackChunkName: "Add" */ '@/components/goods/Add')

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  component: Login // 登陆页面
},
{
  path: '/home', // 主页
  component: Home,
  redirect: '/welcome',
  children: [
    {
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/goods',
      component: Goods
    },
    {
      path: '/goods',
      component: Goods
    },
    {
      path: '/categories',
      component: Cate
    },
    {
      path: '/params',
      component: Params
    },
    {
      path: '/orders',
      component: Oder
    }, {
      path: '/rights',
      component: Rights
    },
    {
      path: '/roles',
      component: Roles
    },
    {
      path: '/reports',
      component: Report
    },
    {
      path: '/users',
      component: Users
    },
    {
      path: '/goods/add',
      component: Add
    }
  ]
}

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
